﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.IO;
using LDSP.BRL;
using BYFW.BOL;
using LDSP.Helper;

namespace LDSP.UI.Admin
{
    public partial class SystemConfig : LDSP.BRL.BaseSysAdminPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (this.RB())
                {
                    if (!IsPostBack)
                    {
                        this.InitDataSelect();
                    }
                }
            }
            catch (Exception ex)
            {
                this.HandlerException(ex);
            }
        }

        protected bool RB()
        {
            return this.AllowSystemUser();
        }

        protected void InitDataSelect()
        {
            this.txt_maxDownloadTimes.Text = BRL.SystemConfig.MaxDownloadTimes.ToString();
            this.txt_invalidAlert.Text = BRL.SystemConfig.InvalidAlertDays.ToString();
            this.txt_expireAlert.Text = BRL.SystemConfig.ExpireAlertDays.ToString();
            this.txt_addNewAlert.Text = BRL.SystemConfig.AddNewAlertDays.ToString();
            this.txt_superUserName.Text = BRL.SystemConfig.SuperUserName;
        }

        protected void ValidateDataBefore()
        {
            if (this.txt_superUserName.Text.Trim() == "")
            {
                throw new CustomException("管理员用户名不能为空！");
            }
        }
        protected void ValidateDataAfter()
        {
            int tmp = this.GetInt(BRL.SystemConfig.MaxDownloadTimes);
            tmp = tmp < 1 ? 1 : tmp;
            BRL.SystemConfig.MaxDownloadTimes = tmp.ToString();
            tmp = this.GetInt(BRL.SystemConfig.InvalidAlertDays);
            tmp = tmp < 1 ? 1 : tmp;
            BRL.SystemConfig.InvalidAlertDays = tmp.ToString();
            tmp = this.GetInt(BRL.SystemConfig.ExpireAlertDays);
            tmp = tmp < 1 ? 1 : tmp;
            BRL.SystemConfig.ExpireAlertDays = tmp.ToString();
            tmp = this.GetInt(BRL.SystemConfig.AddNewAlertDays);
            tmp = tmp < 1 ? 1 : tmp;
            BRL.SystemConfig.AddNewAlertDays = tmp.ToString();
        }
        protected void SetData()
        {
            this.ValidateDataBefore();
            BRL.SystemConfig.MaxDownloadTimes = this.txt_maxDownloadTimes.Text.Trim();
            BRL.SystemConfig.InvalidAlertDays = this.txt_invalidAlert.Text.Trim();
            BRL.SystemConfig.ExpireAlertDays = this.txt_expireAlert.Text.Trim();
            BRL.SystemConfig.AddNewAlertDays = this.txt_addNewAlert.Text.Trim();
            BRL.SystemConfig.SuperUserName = this.txt_superUserName.Text.Trim();
            this.ValidateDataAfter();
        }

        private void BindGrid()
        {
            BRL.Codification ett = new BRL.Codification();
            DataTable dt = ett.GetRecordsByEnterpriseID(0);
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                BRL.Codification tmp = new BRL.Codification();
                tmp.GetProperties(dt.Rows[i]);
                try
                {
                    if (tmp.FileName != "" && File.Exists(tmp.GetDownloadFileFullName()))
                    {
                        dt.Rows.RemoveAt(i);
                        i--;
                    }
                }
                catch { }
            }
            this.grd_list.DataSource = dt;
            this.grd_list.DataBind();
            this.lbl_check.Visible = dt.Rows.Count > 0;
        }

        protected void btn_ok_Click(object sender, EventArgs e)
        {
            try
            {
                this.SetData();
                this.JSAlert("更新成功！");
                this.InitDataSelect();
            }
            catch (Exception ex)
            {
                this.HandlerException(ex);
            }
        }
        protected void btn_clearCache_Click(object sender, EventArgs e)
        {
            try
            {
                TblCacheManager.ClearAll();
                ObjCacheManager.ClearAll();
                this.JSAlert("清除系统缓存成功！");
            }
            catch (Exception ex)
            {
                this.HandlerException(ex);
            }
        }
        protected void btn_clearFile_Click(object sender, EventArgs e)
        {
            try
            {
                string folderName = UIHelper.GetTempFolder();

                //删除临时目录。 此处有权限问题。
                DeleteFiles("*.pdf");
                DeleteFiles("*.xls");

                this.JSAlert("清除系统垃圾成功！");
            }
            catch (Exception ex)
            {
                this.HandlerException(ex);
            }
        }

        private void DeleteFiles(string searchOptions)
        {
            string[] files = Directory.GetFiles(Server.MapPath("~\\TmpImg"), searchOptions);
            foreach (string file in files)
            {
                try
                {
                    File.Delete(file);
                }
                catch (IOException exception)
                {
                    this.JSAlert(exception.Message);
                }
            }
        }
        protected void btn_checkService_Click(object sender, EventArgs e)
        {
            try
            {
                LDSP.Util.CommonService.CheckTimerService();
                this.JSAlert("系统服务检查成功！");
            }
            catch (Exception ex)
            {
                this.HandlerException(ex);
            }
        }
        protected void btn_check_Click(object sender, EventArgs e)
        {
            try
            {
                BindGrid();
            }
            catch (Exception ex)
            {
                this.HandlerException(ex);
            }
        }
        protected void btn_checkPwd_Click(object sender, EventArgs e)
        {
            try
            {
                new SystemUser().UpdatePwdToEncrypted();
                new EnterpriseUser().UpdatePwdToEncrypted();
                this.JSAlert("系统用户和企业用户的密码均已加密完毕！");
            }
            catch (Exception ex)
            {
                this.HandlerException(ex);
            }
        }

        protected void grd_list_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            try
            {
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    Codification ett = new Codification();
                    ett.GetProperties(((DataRowView)e.Row.DataItem).Row);
                    this.ShowSequenceNumber(e.Row, 1);
                    e.Row.Cells[2].Text = ett.Catalog.Name;
                }
            }
            catch (Exception ex)
            {
                this.HandlerException(ex);
            }
        }

    }

}